package cn.tedu.ivosback.user.service.impl;

import cn.tedu.ivosback.base.exception.ServiceException;
import cn.tedu.ivosback.base.response.StatusCode;
import cn.tedu.ivosback.user.mapper.UserMapper;
import cn.tedu.ivosback.user.pojo.dto.UserLoginParam;
import cn.tedu.ivosback.user.pojo.dto.UserQuery;
import cn.tedu.ivosback.user.pojo.dto.UserSaveParam;
import cn.tedu.ivosback.user.pojo.entity.User;
import cn.tedu.ivosback.user.pojo.vo.UserVO;
import cn.tedu.ivosback.user.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    UserMapper userMapper;

    @Override
    public UserVO login(UserLoginParam userLoginParam) {
        UserVO userVO = userMapper.selectByUsername(userLoginParam.getUsername());
        if (userVO == null) {
            throw new ServiceException(StatusCode.USERNAME_ERROR);
        }
        if (!userVO.getPassword().equals(userLoginParam.getPassword())) {
            throw new ServiceException(StatusCode.PASSWORD_ERROR);
        }
        return userVO;
    }

    @Override
    public void saveUser(UserSaveParam userSaveParam) {
        User user = new User();
        BeanUtils.copyProperties(userSaveParam, user);
        //如果没有id值,说明是新增操作
        if (user.getId() == null) {
            //注意:员工不是自己注册的，所以需要设置初始密码
            user.setPassword("123456");
            user.setCreateTime(new Date());
            userMapper.insert(user);
        } else {//如果有id说明是编辑用户操作,走更新方法
            user.setUpdateTime(new Date());
            userMapper.update(user);
        }
    }

    @Override
    public List<UserVO> selectUser(UserQuery userQuery) {
        return userMapper.selectUser(userQuery);
    }

    @Override
    public void resetPassword(Long userId) {
        User user = new User();
        user.setId(userId);
        user.setPassword("root");
        user.setUpdateTime(new Date());
        userMapper.update(user);
    }

    @Override
    public void updateStatus(Long userId, String status) {
        User user = new User();
        user.setId(userId);
        user.setStatus(status);
        user.setUpdateTime(new Date());
        userMapper.update(user);
    }

    @Override
    public void deleteUser(Long userId) {
        userMapper.deleteById(userId);
    }

    @Override
    public List<UserVO> selectAuditUser(Long parentId) {
        List<UserVO> userVOS = new ArrayList<>();
        UserVO auditUser1 = userMapper.selectByParentId(parentId);
        userVOS.add(auditUser1);
        if (auditUser1 != null && auditUser1.getParentId() != null) {
            UserVO auditUser2 = userMapper.selectByParentId(auditUser1.getParentId());
            userVOS.add(auditUser2);
        }
        return userVOS;
    }

    @Override
    public UserVO selectById(Long userId) {
        return userMapper.selectById(userId);
    }
}

